package com.noahplat.person.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.noahplat.common.utils.NoahEnum;
import com.noahplat.common.utils.RedisUtils;
import com.noahplat.common.utils.Result;
import com.noahplat.common.utils.StatusCode;
import com.noahplat.person.mapper.FirstDepartMapper;
import com.noahplat.person.mapper.MedicalRecordMapper;
import com.noahplat.person.mapper.SecondDepartMapper;
import com.noahplat.person.pojo.FirstDepart;
import com.noahplat.person.pojo.MedicalRecord;
import com.noahplat.person.pojo.SecondDepart;
import com.noahplat.person.service.DepartService;
import com.noahplat.person.utils.DepartDto;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.beans.Transient;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: DepartServiceImpl
 * @Description: 科室service实现类
 * @Author 李景星
 * @Date 2021/3/6
 * @Version 1.0
 */
@Service
public class DepartServiceImpl implements DepartService {

    @Autowired
    private FirstDepartMapper firstDepartMapper;
    @Autowired
    private SecondDepartMapper secondDepartMapper;
    @Autowired
    private MedicalRecordMapper medicalRecordMapper;
    @Autowired
    private RedisUtils redisUtils;


    /**
     * 查询所有分类信息
     * @return
     */
    @Override
    public Result getAllDepart() {
        /*先去redis中查询，若redis中无数据，去数据库查询*/

        //处理一级分类
        List<FirstDepart> firstDeparts = new ArrayList<FirstDepart>();
        if(redisUtils.hasKey(NoahEnum.FIRSTDEPARTDIR.getCode()+NoahEnum.FIRSTDEPARTKEY.getCode())){
            firstDeparts = (List<FirstDepart>)(List)redisUtils.lGet(NoahEnum.FIRSTDEPARTDIR.getCode() + NoahEnum.FIRSTDEPARTKEY.getCode(), 0, -1);
        }else {
            firstDeparts = firstDepartMapper.selectAll();
            for (FirstDepart firstDepart : firstDeparts) {
                redisUtils.lSet(NoahEnum.FIRSTDEPARTDIR.getCode()+NoahEnum.FIRSTDEPARTKEY.getCode(), firstDepart);
            }
        }

        //处理二级分类
        List<SecondDepart> secondDeparts = new ArrayList<SecondDepart>();
        if(redisUtils.hasKey(NoahEnum.SECONDDEPARTDIR.getCode()+NoahEnum.SECONDDEPARTKEY.getCode())){
            secondDeparts = (List<SecondDepart>)(List)redisUtils.lGet(NoahEnum.SECONDDEPARTDIR.getCode() + NoahEnum.SECONDDEPARTKEY.getCode(), 0, -1);
        }else {
            secondDeparts = secondDepartMapper.selectAll();
            for (SecondDepart secondDepart : secondDeparts) {
                redisUtils.lSet(NoahEnum.SECONDDEPARTDIR.getCode()+NoahEnum.SECONDDEPARTKEY.getCode(), secondDepart);
            }
        }

        //封装返回前端数据
        List<DepartDto> departDtos = new ArrayList<>();
        for (FirstDepart firstDepart : firstDeparts) {
            DepartDto departDto = new DepartDto();
            List<SecondDepart> secondDepartList = new ArrayList<>();
            for (SecondDepart secondDepart : secondDeparts) {
                if(firstDepart.getId().equals(secondDepart.getFid())){
                    secondDepartList.add(secondDepart);
                }
            }
            departDto.setFirstDepart(firstDepart);
            departDto.setSecondDeparts(secondDepartList);
            departDtos.add(departDto);
        }

        return new Result(true, StatusCode.OK, "查询科室分类信息成功", departDtos);
    }

    /**
     * 获得二级分类信息
     * @param page
     * @param limit
     * @return
     */
    @Override
    public Result getSDepart(Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        List<SecondDepart> list = secondDepartMapper.selectDepart();
        if(ObjectUtils.isEmpty(list)){
            return new Result(false,StatusCode.ERROR,"暂无数据");
        }
        PageInfo<SecondDepart> pageInfo = new PageInfo<>(list);
        return new Result(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /**
     * 添加一级分类信息
     * @param firstDepart
     * @return
     */
    @Transactional
    @Override
    public Result addFDepart(FirstDepart firstDepart){
        Example example = new Example(FirstDepart.class);
        example.createCriteria().andEqualTo("fdepart");
        FirstDepart firstDepart1 = firstDepartMapper.selectOneByExample(example);
        if(!ObjectUtils.isEmpty(firstDepart1)){
            return new Result(false,StatusCode.ERROR,"已存在该分类");
        }
        int res = firstDepartMapper.insertSelective(firstDepart);
        if(res == 0){
            return new Result(false,StatusCode.ERROR,"添加失败");
        }
        boolean inres = redisUtils.lSet(NoahEnum.FIRSTDEPARTDIR.getCode()+NoahEnum.FIRSTDEPARTKEY.getCode(), firstDepart);
        if(!inres) {
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
        return new Result(true,StatusCode.OK,"添加一级分类成功");
    }

    /**
     * 添加分类信息
     * @param secondDepart
     * @return
     */
    @Transactional
    @Override
    public Result addSDepart(SecondDepart secondDepart){

        Example example1 = new Example(SecondDepart.class);
        example1.createCriteria().andEqualTo("sdepart",secondDepart.getSdepart()).andEqualTo("fid",secondDepart.getFid());
        SecondDepart secondDepart2 = secondDepartMapper.selectOneByExample(example1);
        if(!ObjectUtils.isEmpty(secondDepart2)){
            return new Result(false,StatusCode.ERROR,"已存在该分类");
        }
        int res = secondDepartMapper.insertSelective(secondDepart);
        if(res == 0){
            return new Result(false,StatusCode.ERROR,"添加失败");
        }
        boolean inres = redisUtils.lSet(NoahEnum.SECONDDEPARTDIR.getCode()+NoahEnum.SECONDDEPARTKEY.getCode(), secondDepart);
        if(!inres){
            return new Result(false,StatusCode.ERROR,"添加失败");
        }
        return new Result(true,StatusCode.OK,"添加分类成功");
    }

    /**
     * 修改一级分类信息
     * @param firstDepart
     * @return
     */
    @Transactional
    @Override
    public Result updateFDepart(FirstDepart firstDepart){
        Example example = new Example(FirstDepart.class);
        example.createCriteria().andEqualTo("fdepart",firstDepart.getFdepart());
        FirstDepart firstDepart1 = firstDepartMapper.selectOneByExample(example);
        if(!ObjectUtils.isEmpty(firstDepart1)){
            return new Result(false,StatusCode.ERROR,"修改失败,已存在该分类");
        }
        int res = firstDepartMapper.updateByPrimaryKeySelective(firstDepart);
        if(res == 0){
            return new Result(false,StatusCode.ERROR,"修改失败");
        }
        if(redisUtils.hasKey(NoahEnum.FIRSTDEPARTDIR.getCode()+NoahEnum.FIRSTDEPARTKEY.getCode())){
            List<Object> list = redisUtils.lGet(NoahEnum.FIRSTDEPARTDIR.getCode()+NoahEnum.FIRSTDEPARTKEY.getCode(),0,-1);
            for(Object object : list){
                FirstDepart firstDepart2 = (FirstDepart) object;
                if(firstDepart2.getId() == firstDepart.getId()){
                    long delres = redisUtils.lRemove(NoahEnum.FIRSTDEPARTDIR.getCode()+NoahEnum.FIRSTDEPARTKEY.getCode(),1,object);
                    if(delres == 0){
                        return new Result(false,StatusCode.ERROR,"删除内存出错");
                    }
                    boolean inres = redisUtils.lSet(NoahEnum.FIRSTDEPARTDIR.getCode()+NoahEnum.FIRSTDEPARTKEY.getCode(),firstDepart);
                    if(!inres){
                        return new Result(false,StatusCode.ERROR,"插入内存出错");
                    }
                    break;
                }
            }
        }else {
            List<FirstDepart> list = firstDepartMapper.selectAll();
            if(ObjectUtils.isEmpty(list)){
                return new Result(false,StatusCode.ERROR,"暂无数据");
            }
            for(FirstDepart firstDepart2 : list){
                if(firstDepart2.getId() == firstDepart.getId()){
                    redisUtils.lSet(NoahEnum.FIRSTDEPARTDIR.getCode()+NoahEnum.FIRSTDEPARTKEY.getCode(),firstDepart);
                    continue;
                }
                redisUtils.lSet(NoahEnum.FIRSTDEPARTDIR.getCode()+NoahEnum.FIRSTDEPARTKEY.getCode(),firstDepart2);
            }
        }
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /**
     * 修改二级分类信息
     * @param secondDepart
     * @return
     */
    @Transactional
    @Override
    public Result updateSDepart(SecondDepart secondDepart){
        Example example = new Example(SecondDepart.class);
        example.createCriteria().andEqualTo("fid",secondDepart.getFid()).andEqualTo("sdepart",secondDepart.getSdepart());
        SecondDepart secondDepart1 = secondDepartMapper.selectOneByExample(example);
        if(!ObjectUtils.isEmpty(secondDepart1)){
            return new Result(false,StatusCode.ERROR,"修改失败,本一级分类已存在改二级分类");
        }
        int res = secondDepartMapper.updateByPrimaryKeySelective(secondDepart);
        if(res == 0){
            return new Result(false,StatusCode.ERROR,"修改失败");
        }
        if(redisUtils.hasKey(NoahEnum.SECONDDEPARTDIR.getCode() + NoahEnum.SECONDDEPARTKEY.getCode())){
            List<Object> list = redisUtils.lGet(NoahEnum.SECONDDEPARTDIR.getCode() + NoahEnum.SECONDDEPARTKEY.getCode(), 0, -1);
            for(Object object : list){
                SecondDepart secondDepart2 = (SecondDepart) object;
                if(secondDepart2.getId() == secondDepart.getId()){
                    long delres = redisUtils.lRemove(NoahEnum.SECONDDEPARTDIR.getCode() + NoahEnum.SECONDDEPARTKEY.getCode(),1,object);
                    if(delres == 0){
                        return new Result(false,StatusCode.ERROR,"删除内存失败");
                    }
                    boolean inres = redisUtils.lSet(NoahEnum.SECONDDEPARTDIR.getCode() + NoahEnum.SECONDDEPARTKEY.getCode(),secondDepart);
                    if(!inres){
                        return new Result(false,StatusCode.ERROR,"插入内存出错");
                    }
                    break;
                }
            }
        }else {
            List<SecondDepart> list = secondDepartMapper.selectAll();
            if(ObjectUtils.isEmpty(list)){
                return new Result(false,StatusCode.ERROR,"暂无数据");
            }
            for(SecondDepart secondDepart2 : list){
                if(secondDepart2.getId() == secondDepart.getId()){
                    redisUtils.lSet(NoahEnum.SECONDDEPARTDIR.getCode() + NoahEnum.SECONDDEPARTKEY.getCode(),secondDepart);
                    continue;
                }
                redisUtils.lSet(NoahEnum.SECONDDEPARTDIR.getCode() + NoahEnum.SECONDDEPARTKEY.getCode(),secondDepart2);
            }
        }
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /**
     * 分页获取一级分类信息
     * @param page
     * @param limit
     * @return
     */
    @Override
    public Result getAdminFDepart(Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        List<FirstDepart> list = firstDepartMapper.selectAll();
        if(ObjectUtils.isEmpty(list)){
            return new Result(false,StatusCode.ERROR,"暂无数据");
        }
        PageInfo<FirstDepart> pageInfo = new PageInfo<>(list);
        return new Result(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /**
     * 获得一级分类信息
     * @return
     */
    @Override
    public Result getFDepart(){
        List<FirstDepart> list = new ArrayList<>();
        if(redisUtils.hasKey(NoahEnum.FIRSTDEPARTDIR.getCode()+NoahEnum.FIRSTDEPARTKEY.getCode())){
            list = (List<FirstDepart>)(List)redisUtils.lGet(NoahEnum.FIRSTDEPARTDIR.getCode()+NoahEnum.FIRSTDEPARTKEY.getCode(),0,-1);
            if(ObjectUtils.isEmpty(list)){
                return new Result(false,StatusCode.ERROR,"暂无数据");
            }
        }else {
            list = firstDepartMapper.selectAll();
            if(ObjectUtils.isEmpty(list)){
                return new Result(false,StatusCode.ERROR,"暂无数据");
            }
            for(FirstDepart firstDepart : list){
                redisUtils.lSet(NoahEnum.FIRSTDEPARTDIR.getCode()+NoahEnum.FIRSTDEPARTKEY.getCode(),firstDepart);
            }
        }
        return new Result(true,StatusCode.OK,"查询成功",list);
    }

    /**
     * 通过ID获取一级分类详细信息
     * @param id
     * @return
     */
    @Override
    public Result getFDepartById(Long id){
        Result result = getFDepart();
        if(!result.isFlag()){
            return new Result(false,StatusCode.ERROR,"查询失败");
        }
        List<FirstDepart> list = (List<FirstDepart>) result.getData();
        for(FirstDepart firstDepart : list){
            if(firstDepart.getId() == id){
                return new Result(true,StatusCode.OK,"查询成功",firstDepart);
            }
        }
        return new Result(false,StatusCode.ERROR,"查询失败");
    }

    /**
     * 删除一级分类信息
     * @param id
     * @return
     */
    @Transactional
    @Override
    public Result delFDepart(Long id){
        Example example1 = new Example(MedicalRecord.class);
        example1.createCriteria().andEqualTo("fdepart",id);
        List<MedicalRecord> list2 = medicalRecordMapper.selectByExample(example1);

        Example example = new Example(SecondDepart.class);
        example.createCriteria().andEqualTo("fid",id);
        List<SecondDepart> list = secondDepartMapper.selectByExample(example);
        if(!ObjectUtils.isEmpty(list) || !ObjectUtils.isEmpty(list2)){
            return new Result(false,StatusCode.ERROR,"删除失败,正在使用");
        }
        int res = firstDepartMapper.deleteByPrimaryKey(id);
        if(res == 0){
            return new Result(false,StatusCode.ERROR,"删除失败");
        }
        if(redisUtils.hasKey(NoahEnum.FIRSTDEPARTDIR.getCode()+NoahEnum.FIRSTDEPARTKEY.getCode())){
            List<Object> list1 = redisUtils.lGet(NoahEnum.FIRSTDEPARTDIR.getCode()+NoahEnum.FIRSTDEPARTKEY.getCode(),0,-1);
            if(ObjectUtils.isEmpty(list1)){
                return new Result(false,StatusCode.ERROR,"暂无数据");
            }
            for(Object object : list1){
                FirstDepart firstDepart = (FirstDepart) object;
                if(firstDepart.getId() == id){
                    long delres = redisUtils.lRemove(NoahEnum.FIRSTDEPARTDIR.getCode()+NoahEnum.FIRSTDEPARTKEY.getCode(),1,object);
                    if(delres == 0){
                        return new Result(false,StatusCode.ERROR,"删除内存失败");
                    }
                    break;
                }
            }
        }else {
            List<FirstDepart> list1 = firstDepartMapper.selectAll();
            if(ObjectUtils.isEmpty(list1)){
                return new Result(false,StatusCode.ERROR,"暂无数据");
            }
            for(FirstDepart firstDepart : list1){
                if(firstDepart.getId() == id){
                    continue;
                }
                redisUtils.lSet(NoahEnum.FIRSTDEPARTDIR.getCode()+NoahEnum.FIRSTDEPARTKEY.getCode(),firstDepart);
            }
        }
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /**
     * 获得根据id二级分类信息
     * @param id
     * @return
     */
    @Override
    public Result getSDepartById(Long id){
        List<SecondDepart> list = new ArrayList<>();
        if(redisUtils.hasKey(NoahEnum.SECONDDEPARTDIR.getCode() + NoahEnum.SECONDDEPARTKEY.getCode())){
            list = (List<SecondDepart>)(List)redisUtils.lGet(NoahEnum.SECONDDEPARTDIR.getCode() + NoahEnum.SECONDDEPARTKEY.getCode(), 0, -1);
            if(ObjectUtils.isEmpty(list)){
                return new Result(false,StatusCode.ERROR,"暂无数据");
            }
        }else {
            list = secondDepartMapper.selectAll();
            if(ObjectUtils.isEmpty(list)){
                return new Result(false,StatusCode.ERROR,"暂无数据");
            }
            for(SecondDepart secondDepart : list){
                redisUtils.lSet(NoahEnum.SECONDDEPARTDIR.getCode() + NoahEnum.SECONDDEPARTKEY.getCode(),secondDepart);
            }
        }
        for(SecondDepart secondDepart : list){
            if(secondDepart.getId() == id){
                return new Result(true,StatusCode.OK,"查询成功",secondDepart);
            }
        }
        return new Result(false,StatusCode.ERROR,"暂无数据");
    }

    /**
     * 删除二级分类信息
     * @param id
     * @return
     */
    @Transactional
    @Override
    public Result delSDepart(Long id){
        Example example1 = new Example(MedicalRecord.class);
        example1.createCriteria().andEqualTo("sdepart",id);
        List<MedicalRecord> list2 = medicalRecordMapper.selectByExample(example1);
        if(!ObjectUtils.isEmpty(list2)){
            return new Result(false,StatusCode.ERROR,"删除失败,正在使用");
        }
        int res = secondDepartMapper.deleteByPrimaryKey(id);
        if(res == 0){
            return new Result(false,StatusCode.ERROR,"删除失败");
        }
        if(redisUtils.hasKey(NoahEnum.SECONDDEPARTDIR.getCode() + NoahEnum.SECONDDEPARTKEY.getCode())){
            List<Object> list = redisUtils.lGet(NoahEnum.SECONDDEPARTDIR.getCode() + NoahEnum.SECONDDEPARTKEY.getCode(), 0, -1);
            if(ObjectUtils.isEmpty(list)){
                return new Result(false,StatusCode.ERROR,"删除失败");
            }
            for(Object object : list){
                SecondDepart secondDepart = (SecondDepart) object;
                if(secondDepart.getId() == id){
                    long delres = redisUtils.lRemove(NoahEnum.SECONDDEPARTDIR.getCode() + NoahEnum.SECONDDEPARTKEY.getCode(),1,object);
                    if(delres == 0){
                        return new Result(false,StatusCode.ERROR,"删除内存错误");
                    }
                    break;
                }
            }
        }else {
            List<SecondDepart> list = secondDepartMapper.selectAll();
            for(SecondDepart secondDepart : list){
                if(secondDepart.getId() == id){
                    continue;
                }
                redisUtils.lSet(NoahEnum.SECONDDEPARTDIR.getCode() + NoahEnum.SECONDDEPARTKEY.getCode(),secondDepart);
            }
        }
        return new Result(true,StatusCode.OK,"删除成功");
    }


}
